let fetch = require("../../utils/fetch");
let app = getApp();
Page({
    data: {
        imageUrl: app.globalData.imageUrl,
        level:"00",
        avatarUrl:"",
        nickName:"小麦铺粉丝",
        winHeight: "",//窗口高度
        currentTab: 0, //预设当前项的值
        allList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        unpayList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        succList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false
        }
    },
    getUserInfo: function () {
        
        this.setData({
            imageUrl: app.globalData.imageUrl,
            shopCode:app.globalData.common.shopCode,
            level:app.globalData.userInfo.level,
            avatarUrl:app.globalData.userInfo.avatarUrl,
            nickName:app.globalData.userInfo.nickName
        })
    },

    // 滚动切换标签样式
    switchTab: function (e) {
        console.log(e.detail.current)
        this.setData({
            currentTab: e.detail.current
        });
        this.judgeSwitch(e.detail.current)

        // this.checkCor();
    },
    getLevel: function () {
        fetch.loadingText(),
            fetch.getLevel().then((response) => {
                fetch.hideLoadingText()
                const res = response.data;
                if (res.code == 200) {
                    this.setData({
                        level: res.obj.level
                    })
                } else {
                    reject(res)
                }
            }).catch((e) => {
                fetch.hideLoadingText()
                fetch.showMyToast(this, e.data.des)
            })
    },
    judgeSwitch: function (current) {
        if (current == 0 && this.data.allList.list.length == 0) {
                this.loadMore(100, "allList") 
        } else if (current == 1 && this.data.unpayList.list.length == 0) {
                this.loadMore(0, "unpayList")
            
        } else if (current == 2 && this.data.succList.list.length == 0) {
                this.loadMore(2, "succList")
        }

    },
    // 点击标题切换当前页时改变样式
    switchNav: function (e) {
        var cur = e.currentTarget.dataset.current;
        console.log(e)
        if (this.data.currentTaB == cur) { return false; }
        else {
            this.setData({
                currentTab: cur
            })
        }
        this.judgeSwitch(cur)
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    onLoad: function (options) {
        console.log('onLoad')
        console.log(app.globalData)
        this.getUserInfo();
        console.log(this.data.userInfo)
        var that = this;
        //  高度自适应
        this.heightAuto()
        this.getLevel()
        if(app. globalData.common.selfPay){
            return
        }else{
             // 加载全部状态的订单
        this.onShowMethod(100, 1, 'allList')
        }
       

    },
    heightAuto: function () {
        var that=this;
        wx.getSystemInfo({
            success:function(res){
                var clientHeight = res.windowHeight,
                clientWidth = res.windowWidth,
                rpxR = 750 / clientWidth;
            var calc = clientHeight * rpxR - 352;
            that.setData({
                winHeight: calc
            });
            }
        })

    },
    lower: function (e) {
        console.log('lower', e.currentTarget.dataset.list)
        this.loadMore(e.currentTarget.dataset.status, e.currentTarget.dataset.list)

    },
    loadMore: function (orderStatus, lists) {
        if (this.data[lists].isLast || this.data[lists].isLoading) {
            return
        } else {
            this.getOrderList(orderStatus, this.data[lists].curPage, lists)
        }

    },
    onShowMethod: function (orderStatus, pageNum, lists) {

        this.getOrderList(orderStatus, pageNum, lists)

    },
    getOrderList: function (orderStatus, pageNum, lists) {
        fetch.loadingText()
        this.setData({
            [lists]: {
                isLast: this.data[lists].isLast,
                curPage: this.data[lists].curPage,
                list: this.data[lists].list,
                isLoading: true
            },
        })
        fetch.getOrders(orderStatus, pageNum).then((response) => {
            fetch.hideLoadingText()
            const res = response.data;
            if (res.code == 200) {
                let list = this.data[lists].list.concat(res.obj.result)
                this.setData({
                    [lists]: {
                        isLast: res.obj.lastPage,
                        curPage: this.data.allList.curPage + 1,
                        list: list,
                        isLoading: false
                    },

                })
            }
            fetch.compatibilityHandler(wx.stopPullDownRefresh)


        }).catch((err) => {
            fetch.hideLoadingText()
            fetch.showMyToast(this, err.data.des)
        })
    },

    onReady: function () {
        console.log('onReady')
    },
    onShow: function () {
        //判断当从支付页跳转后，刷新订单
        if(app. globalData.common.selfPay){
            this.initData()
            this.onShowMethod(100, 1, 'allList')
            app. globalData.common.selfPay=null;
        }
    },
    onPullDownRefresh(){
        this.initData();
        this.onShowMethod(100, 1, 'allList');
    },
    initData:function(){
        this.setData({
            currentTab: 0, //预设当前项的值
            allList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false
    
            },
            unpayList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false
    
            },
            succList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false
            }
        })
    },
    onHide: function () {
        // this.setData({
        // 	orderStatus: 100,
        // 	orderList: [],
        // 	totalPage: 0,
        // 	pageNum: 1,
        // })
    },
    onUnload: function () {
        console.log('onUnload')
    },

    checkNetwork: function () {
        fetch.NetworkType().then((res) => {
            console.log(res);
            const networkType = res.networkType;
            console.log(networkType)
            if (networkType == '2g' || networkType == 'none' || networkType == 'unknown') {
                const self = this;
                fetch.showMyToast(self, `当前网络状况为：${networkType}，可能会影响使用效果`)
            }
        })
    },
})